<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2021/11/16
  Time: 13:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/lib/layui-v2.5.5/css/layui.css"
          media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <%--搜索区--%>
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">部门名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="deptName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i>搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-warm" lay-submit
                                    lay-filter=""><i class="layui-icon layui-icon-refresh"></i>重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <%--工具栏--%>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i
                        class="layui-icon layui-icon-add-circle"></i>添加
                </button>
            </div>
        </script>
        <%--数据区--%>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <%--数据工具区--%>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>

        <%-- 添加和修改窗口 --%>
        <div style="display: none;padding: 5px" id="addOrUpdateWindow">
            <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                <%-- 隐藏域--%>
                <input type="text" name="id" hidden>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="deptName" lay-verify="required" autocomplete="off"
                               placeholder="请输入部门名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" lay-verify="required" autocomplete="off" placeholder="请输入部门地址"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门备注</label>
                    <div class="layui-input-block">
                        <textarea class="layui-textarea" name="remark" id="content"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-row layui-col-xs12">
                    <div class="layui-input-block" style="text-align: center;">
                        <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span
                                class="layui-icon layui-icon-add-1"></span>提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-warm"><span
                                class="layui-icon layui-icon-refresh-1"></span>重置
                        </button>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>
<script src="${pageContext.request.contextPath}/static/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer
        ;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '${pageContext.request.contextPath}/admin/dept/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'id', width: 120, title: '部门ID', sort: true, align: "center"},
                {field: 'deptName', width: 200, title: '部门名称', align: "center"},
                {field: 'address', width: 280, title: '部门地址', align: "center"},
                {field: 'createDate', width: 200, title: '创建时间', align: "center"},
                {field: 'remark', title: '备注', minWidth: 150, align: "center"},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            // skin: 'line'
            // done:function (res,curr,count) {
            //     if(curr>1 && res.data.length==0){
            //         var pageValue=curr-1;
            //         tableIns.reload({
            //             page:{curr:pageValue}
            //         })
            //     }
            // }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            console.log(data)
            //执行搜索重载
            tableIns.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAddWindow();
            }
        });

        // 监听数据操作
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openUpdateWindow(data);
            } else if (obj.event === 'delete') {
                deleteById(data);
            }
        });

        let openIndex;
        let url;

        //打开添加部门窗口
        function openAddWindow() {
            openIndex = layer.open({
                type: 1,//打开类型
                title: "添加部门",//窗口标题
                area: ["800px", "400px"],//窗口宽高
                content: $("#addOrUpdateWindow"),//引用的内容窗口
                success: function () {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    //添加的提交请求
                    url = "/admin/dept/addDept";
                }
            });
        }

        //打开部门更新窗口
        function openUpdateWindow(data) {
            openIndex = layer.open({
                type: 1,//打开类型
                title: "修改部门",//窗口标题
                area: ["800px", "400px"],//窗口宽高
                content: $("#addOrUpdateWindow"),//引用的内容窗口
                success: function () {
                    form.val("dataFrm", data)
                    //添加的提交请求
                    url = "/admin/dept/updateDept";
                }
            });
        }

        //删除部门
        function deleteById(data) {
            $.get("${pageContext.request.contextPath}/admin/employee/checkDeptHasEmployee", {"id": data.id}, function (result) {
                if (result.exist) {
                    //    如果部门下存在员工不删除该部门
                    layer.msg(result.message)
                } else {
                    //    如果部门下无员工，可删除
                    layer.confirm('确定删除 ['+data.deptName+'] 么?', {icon: 3, title: '提示'}, function (index) {
                        //do something
                        $.post("${pageContext.request.contextPath}/admin/dept/deleteDept", {"deptId": data.id}, function (result) {
                            if (result.success) {
                                tableIns.reload();
                            }
                            layer.msg(result.message)
                        },"json")
                        layer.close(index);
                    });

                }
            },"json")
        }

        //监听部门提交事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (result) {
                //添加成功
                if (result.success) {
                    //关闭添加窗口
                    layer.close(openIndex);
                    //刷新表格数据
                    tableIns.reload();
                }
                //弹出提示信息
                layer.msg(result.message)
            }, "json")
            //禁止页面刷新
            return false;
        })


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });


    });
</script>

</body>
</html>